package com.metaera.app.ui.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.controller.core.IPaginationPresenter;
import com.metaera.app.controller.core.SimplePaginationPresenter;
import com.metaera.app.controller.presenter.HistoryPresenter;
import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.data.DataRepository;
import com.metaera.app.data.model.BasicListResponse;
import com.metaera.app.data.model.HistoryListResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.adapters.MineHistoryAdapter;
import com.metaera.app.ui.core.BaseRequestListActivity;

import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.Observable;

public class HistoryListActivity extends BaseRequestListActivity<HistoryListResponse> {

    @Override
    protected void initialize(Bundle savedInstanceState) {
        super.initialize(savedInstanceState);

        // find view
        TextView tvAction = findViewById(R.id.tv_action);
        tvAction.setText(R.string.history_clean);

        // network
        HistoryPresenter presenter = new HistoryPresenter();
        presenter.setModelAndView(data -> mPresenter.requestRefresh());

        // listener
        tvAction.setOnClickListener(v -> {
            if (!mAdapter.getData().isEmpty()) {
                presenter.clean();
            }
        });
    }

    @Override
    protected String getActivityTitle() {
        return getString(R.string.browsing_history_text);
    }

    @Override
    protected BaseQuickAdapter<HistoryListResponse, BaseViewHolder> createAdapter(RecyclerView recyclerView) {
        recyclerView.setBackgroundColor(Color.WHITE);
        return new MineHistoryAdapter();
    }

    @Override
    protected IPaginationPresenter<IPaginationView<HistoryListResponse>, HistoryListResponse> createPresenter() {
        return new SimplePaginationPresenter<HistoryListResponse>() {
            @Override
            protected Observable<BasicListResponse<HistoryListResponse>> execute() {
                return DataRepository.getInstance().getHistoryList(mPage, ApiParam.PAGE_SIZE);
            }
        };
    }

}
